package com.yl.auth.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yl.auth.entity.Role;
import com.yl.auth.mapper.JigouzhongxinduanroleMapper;
import com.yl.auth.service.JigouzhongxinduanroleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author L
 * @since 2023-07-19
 */
@Service
public class JigouzhongxinduanroleServiceImpl extends ServiceImpl<JigouzhongxinduanroleMapper, Role> implements JigouzhongxinduanroleService {

    @Resource
    private JigouzhongxinduanroleMapper jigouzhongxinduanroleMapper;

    @Override
    public Page<Role> findAllAndPage(Page<Role> page1) {
        return jigouzhongxinduanroleMapper.findAllAndPage(page1);
    }

    @Override
    public void saveRoleAndPermission(Role role, List<Long> idList) {
        role.setCreateTime(LocalDateTime.now());
        role.setType(2);
        jigouzhongxinduanroleMapper.insert(role);
        Integer roleId = role.getId();
        for (Long perId : idList) {
            jigouzhongxinduanroleMapper.savePermission(roleId,perId);
        }
    }

    @Override
    public List<Long> getRoleById(Long id) {
        List<Long> idList = jigouzhongxinduanroleMapper.getRoleById(id);
        return idList;
    }

    @Override
    public void deleteById(Long id) {
        jigouzhongxinduanroleMapper.deletePerByRoleId(id);
    }

    @Override
    public void updateRpByRoleId(Integer roleId, List<Long> idList) {
        for (Long perId : idList) {
            jigouzhongxinduanroleMapper.savePermission(roleId,perId);
        }
    }

    @Override
    public void deleteRaByRoleId(Long id) {
        jigouzhongxinduanroleMapper.deleteRaByRoleId(id);
    }

    @Override
    public void deleteRpByRoleId(Long id) {
        jigouzhongxinduanroleMapper.deletePerByRoleId(id);
    }

    @Override
    public Role getByIdRole(Integer id) {
        return jigouzhongxinduanroleMapper.getByIdRole(id);
    }
}
